package com.acwing.partition4;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2022/4/7 21:51
 */
public class AC324贿赂FIPA {

    private static final int N = 210, M = N, INF = 0x3f3f3f3f;
    private static Map<String, Integer> map = new HashMap<>(N);
    private static int[] h = new int[N], e = new int[M], ne = new int[M], hw = new int[N], size = new int[N], din = new int[N];
    private static int[][] f = new int[N][N];
    private static int n, m, idx = 0, id = 1;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        while (true) {
            String[] ss = reader.readLine().split(" ");
            if ("#".equals(ss[0])) break;
            reset();
            n = Integer.parseInt(ss[0]);
            m = Integer.parseInt(ss[1]);
            for (int i = 0; i < n; i++) {
                ss = reader.readLine().split(" ");
                map.put(ss[0], id++);
                int u = id - 1;
                hw[u] = Integer.parseInt(ss[1]);
                for (int j = 2; j < ss.length; j++) {
                    int v = -1;
                    if (map.containsKey(ss[j])) v = map.get(ss[j]);
                    if (v == -1) {
                        v = id;
                        map.put(ss[j], id++);
                    }
                    add(u, v);
                }
            }
            //接入超级源点，将森林变成一颗树
            for (int i = 1; i < id; i++) {
                if (din[i] == 0) add(0, i);
            }
            for (int i = 0; i < N; i++) Arrays.fill(f[i], INF);
            for (int i = 0; i < N; i++) f[i][0] = 0;
            dfssize(0);
            dfs(0);
            writer.write(f[0][m] + "\n");
        }
        writer.flush();
    }

    private static int dfssize(int u) {
        if (size[u] != 0) return size[u];
        size[u] = 1;
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            size[u] += dfssize(v);
        }
        return size[u];
    }

    private static void dfs(int u) {
        for (int i = h[u]; i != -1; i = ne[i]) {
            int v = e[i];
            dfs(v);
            for (int j = size[u]; j >= 0; j--) {
                for (int k = 0; k <= j; k++) {
                    f[u][j] = Math.min(f[u][j], f[u][j - k] + f[v][k]);
                }
            }
        }
        if (u != 0) {
            //如果选择当前节点的话可以不用选子节点
            for (int i = size[u]; i >= 1; i--) {
                f[u][i] = Math.min(f[u][i], hw[u]);
            }
        }
    }

    private static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
        din[b]++;
    }

    private static void reset() {
        idx = 0;
        id = 1;
        map.clear();
        for (int i = 0; i < N; i++) {
            h[i] = -1;
            din[i] = size[i] = 0;
            Arrays.fill(f[i], 0);
        }
    }
}
